﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E2_WEBSITE.Code.Admin;
using E2_WEBSITE.Code;
using System.Web.Security;

public partial class cp_admin_acc_view : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
        {
            WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
            return;
        }
        try
        {
            if (gridListUsers.Rows.Count > 0)
            {
                UserController controller = new UserController();
                for (int i = 0; i < gridListUsers.Rows.Count; i++)
                {
                    CheckBox cb = gridListUsers.Rows[i].FindControl("UserSelector") as CheckBox;
                    if (cb != null && cb.Checked)
                    {
                        int uid = Convert.ToInt32(gridListUsers.DataKeys[i].Value);
                        controller.DeleteUser(uid);
                    }
                }
                Page.Response.Redirect("~/cp/admin/user/view.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect(Utilities.ErrorPagePath);
            throw;
        }
    }

    protected void btnResetPassword_Click(object sender, EventArgs e)
    {
        if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
        {
            WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
            return;
        }
        try
        {
            if (gridListUsers.Rows.Count > 0)
            {
                UserController controller = new UserController();
                string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(Utilities.DefaultPassword, "sha1");

                for (int i = 0; i < gridListUsers.Rows.Count; i++)
                {
                    CheckBox cb = gridListUsers.Rows[i].FindControl("UserSelector") as CheckBox;
                    if (cb != null && cb.Checked)
                    {
                        int uid = Convert.ToInt32(gridListUsers.DataKeys[i].Value);
                        controller.ResetPassword(uid, hashedPassword);
                    }
                }
                Page.Response.Redirect("~/cp/admin/user/view.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect(Utilities.ErrorPagePath);
            throw;
        }
    }

    protected void btnSelectAll_Click(object sender, EventArgs e)
    {
        ToggleUserSelector(true);
    }

    protected void btnDeselectAll_Click(object sender, EventArgs e)
    {
        ToggleUserSelector(false);
    }

    private void ToggleUserSelector(bool checkState)
    {
        if (gridListUsers.Rows.Count > 0)
        {
            for (int i = 0; i < gridListUsers.Rows.Count; i++)
            {
                CheckBox cb = gridListUsers.Rows[i].FindControl("UserSelector") as CheckBox;
                if (cb != null)
                {
                    cb.Checked = checkState;
                }
            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
        {
            WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
            return;
        }
        Page.Response.Redirect("~/cp/admin/user/adduser.aspx");
    }
}
